package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfPackersManageActivity2;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * @author 17389207202
 * @date 2021/3/8
 * description:工队列表的adapter
 */
public class SelfPackersManageAdapter extends BaseRecycleAdapter<FindPackersEntity> {


    public SelfPackersManageAdapter(SelfPackersManageActivity2 selfPackersManageActivity2, List<FindPackersEntity> data, int userType) {
        super(selfPackersManageActivity2, R.layout.item_self_packers_manage, data);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        TextView btnEdit = holder.itemView.findViewById(R.id.btn_edit);
        TextView btnDelete = holder.itemView.findViewById(R.id.btn_delete);
        LinearLayout llItems = holder.itemView.findViewById(R.id.ll_item);
        //详情跳转
        llItems.setOnClickListener(v -> workQueueTableDispose.itemData(position));
        //编辑
        btnEdit.setOnClickListener(v -> workQueueTableDispose.edit(position));
        //删除
        btnDelete.setOnClickListener(v -> workQueueTableDispose.delete(position));

    }

    @Override
    public void convert(ItemViewHolder holder, FindPackersEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        RoundedImageView ivLogo = holder.itemView.findViewById(R.id.iv_logo);
        TextView lvTeamPerson = holder.itemView.findViewById(R.id.lv_team_person);
        TextView tvPackersTitle = holder.itemView.findViewById(R.id.tv_packers_title);

        TextView tvDepartment = holder.itemView.findViewById(R.id.tv_department);
        String department = "";
        if (entity.getTeamType() == 1){
            if (TextUtils.isEmpty(entity.getAffiliatedUnitPic())){
                department = "暂无隶属单位";
            }else if (entity.getAffiliatedUnitPic().contains("https")){
                department = "暂无隶属单位";
            }else {
                department = entity.getAffiliatedUnitPic();
            }
        }else if (entity.getTeamType() == 0){
            if (TextUtils.isEmpty(entity.getLegalComName())){
                department = "暂无隶属单位";
            }else if (entity.getLegalComName().contains("https")){
                department = "暂无隶属单位";
            }else {
                department = entity.getLegalComName();
            }
        }
        tvDepartment.setText(department);

        if (null != entity.getAvatarAddress() && !TextUtils.isEmpty(entity.getAvatarAddress())) {
            RequestOptions options = new RequestOptions()
                    .placeholder(entity.getTeamType() == 0 ? R.mipmap.construction_team_detail: R.mipmap.professional_crew_detail)
                    .transforms(new CenterCrop(), new RoundedCorners(8));
            Glide.with(context).load(entity.getAvatarAddress()).apply(options).into(ivLogo);
        } else {
            Glide.with(context).load(entity.getTeamType() == 0 ? R.mipmap.construction_team_detail: R.mipmap.professional_crew_detail).into(ivLogo);
        }
        if (TextUtils.isEmpty(entity.getTeamNumber()) || context.getString(R.string.app_zero).equals(entity.getTeamNumber()) || "0人".equals(entity.getTeamNumber())) {
            lvTeamPerson.setVisibility(View.GONE);
        } else {
            lvTeamPerson.setVisibility(View.VISIBLE);
        }
        String[] strArray = entity.getCategory().trim().replaceAll(",","\\|").split("\\|");
        tvPackersTitle.setText(String.format("%s-%s-%s",entity.getContact() , strArray[strArray.length - 1] , entity.getTeamType() == 0?"施工队":entity.getTeamType()==1?"班组工队":"承包商"));
    }


    private WorkQueueTable workQueueTableDispose;

    public void setWorkQueueTableDispose(WorkQueueTable workQueueTableDispose) {
        this.workQueueTableDispose = workQueueTableDispose;
    }

    public interface WorkQueueTable {
        void itemData(int position);

        void edit(int position);

        void delete(int position);

    }

}
